package com.woniuxy02;

import com.woniuxy01.Stu;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 作者
 * @create 2025-11-19 9:52
 */
public class Test03 {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Student.class;
        //创建对象，使用无参构造
        Student student = (Student) clazz.newInstance();
        System.out.println(student);

        Constructor constructor1 = clazz.getConstructor();
        Student student1 = (Student) constructor1.newInstance();
        System.out.println(student1);

        Constructor constructor2 = clazz.getConstructor( Integer.class,String.class);
        Student studnet2 = (Student) constructor2.newInstance(1, "zs");
        System.out.println(studnet2);

        Constructor[] constructors = clazz.getConstructors();
        Constructor[] constructors1 = clazz.getDeclaredConstructors();


    }

    private static void test01() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        Method method = clazz.getMethod("study");
        //student.study();
        method.invoke(student );

        Method method1 = clazz.getMethod("study", String.class);
        method1.invoke(student, "java");
        //基本数据类型没有属性和方法，基本数据类.class可以使用
        Method method2 = clazz.getMethod("study", String.class, int.class);
        method2.invoke(student,"java",1);

        Method method3 = clazz.getMethod("test", String.class);
        String str = method3.invoke(student, "abc").toString();
        System.out.println(str);

        Method method4 = clazz.getDeclaredMethod("myprivate");
        method4.setAccessible(true);
        method4.invoke(student);

        Field[] declaredFields = clazz.getDeclaredFields();
        Method[] methods = clazz.getMethods();
    }
}